package debux.apps.mltr.app;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import debux.apps.mltr.model.DatabaseController;
import debux.apps.mltr.model.SongInfo;

public class AlbumDetailsAcitivity extends Activity {
	public static int mAlbumId=-1;
	SongListAdapter adapter;
	ArrayList<SongInfo> songList;
	ListView songListView;
	TextView textViewName;
	TextView textViewGenre;
	TextView textViewReleased;
	TextView textViewLabel;
	ImageView imageAlbumThumb;
	
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.album_details);
		songList = new ArrayList<SongInfo>();
		int albumId = AlbumActivity.albumList.get(mAlbumId).getId();
		DatabaseController.getAllSongsByAlbumId(albumId, songList);
		songListView = (ListView)findViewById(R.id.songListView);
		
		textViewName = (TextView) findViewById(R.id.textViewName);
		textViewGenre = (TextView) findViewById(R.id.textViewGenre);
		textViewLabel = (TextView) findViewById(R.id.textViewLabel);
		textViewReleased = (TextView) findViewById(R.id.textViewReleased);
		textViewName.setText(AlbumActivity.albumList.get(mAlbumId).getName());
		textViewGenre.setText("Genre: " + AlbumActivity.albumList.get(mAlbumId).getGenre());
		textViewLabel.setText("Label: " + AlbumActivity.albumList.get(mAlbumId).getLabel());
		textViewReleased.setText("Released: " + AlbumActivity.albumList.get(mAlbumId).getReleased());
		
		adapter = new SongListAdapter(this, R.layout.album_list_item, songList);
		songListView.setAdapter(adapter);
		songListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(AlbumDetailsAcitivity.this,SongDetailsAcitivity.class);
				SongDetailsAcitivity.songInfo = songList.get(arg2);
				startActivity(intent);
			}
		});
		imageAlbumThumb = (ImageView)findViewById(R.id.imageAlbumThumb);
		switch (albumId){
		case 1:
			imageAlbumThumb.setImageResource(R.drawable.album_thumb_1);
			break;
		case 2:
			imageAlbumThumb.setImageResource(R.drawable.album_thumb_2);
			break;
		case 3:
			imageAlbumThumb.setImageResource(R.drawable.album_thumb_3);
			break;
		case 4:
			imageAlbumThumb.setImageResource(R.drawable.album_thumb_4);
			break;
		case 5:
			imageAlbumThumb.setImageResource(R.drawable.album_thumb_5);
			break;
		case 6:
			imageAlbumThumb.setImageResource(R.drawable.album_thumb_6);
			break;
		case 7:
			imageAlbumThumb.setImageResource(R.drawable.album_thumb_7);
			break;
		case 8:
			imageAlbumThumb.setImageResource(R.drawable.album_thumb_8);
			break;
		case 9:
			imageAlbumThumb.setImageResource(R.drawable.album_thumb_9);
			break;			
		}
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
//		Log.i("DatTX","AlbumDetailsAcitivity: onBackPressed");
//		Intent intent = new Intent(AlbumDetailsAcitivity.this, AlbumActivity.class);
//		AlbumActivity parentActivity = (AlbumActivity) getParent();
//		parentActivity.replaceContentView("activity1", intent,
//				Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
	}
	
	class SongListAdapter extends ArrayAdapter<SongInfo>{
		ArrayList<SongInfo> songs;
		
		public SongListAdapter(Context context, int textViewResourceId,
				ArrayList<SongInfo> objects) {
			super(context, textViewResourceId, objects);
			// TODO Auto-generated constructor stub
			songs = objects;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.album_list_item, null);
            }
            //ImageView albumThumb = (ImageView)v.findViewById(R.id.album_thumb);
            TextView albumName = (TextView)v.findViewById(R.id.album_name);
            TextView albumReleased = (TextView)v.findViewById(R.id.album_released);
            albumName.setText(songs.get(position).getName());
            albumReleased.setText(songs.get(position).getLength());
            return v;
		}
	}		
}
